package com.me.study.javaCore.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * ServerSocketChannel：未完成
 */
public class ServerSocketChannelTests {

    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(new InetSocketAddress(9999));
        serverSocketChannel.configureBlocking(false);

        while(true){
            SocketChannel channel = serverSocketChannel.accept();
            ByteBuffer buf = ByteBuffer.allocate(48);
            int bytesRead = channel.read(buf);
            while (bytesRead != -1) {
                // 将 buffer 切换到读模式
                buf.flip();
                while(buf.hasRemaining()){
                    System.out.print((char) buf.get());
                }
                // 翻转 buffer，使其可写
                buf.clear();
                bytesRead = channel.read(buf);
            }
        }

    }

}
